package dy.spec.edu.week12_class02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FirstActivity extends AppCompatActivity {
    private static final String TAG = "FirstActivity";
    List<Message> messageList =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        sendRequestWithOkHttp();
       /* MessageAdapter adapter=new MessageAdapter(FirstActivity.this,R.layout.activity_message_item,messageList);
        ListView lv=(ListView)findViewById(R.id.lv);
        lv.setAdapter((ListAdapter) adapter);*/
    }

     //发起网络请求
    private void sendRequestWithOkHttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 构造客户端，相当于浏览器
                    OkHttpClient client = new OkHttpClient();
                    // 构造请求，相当于在浏览器里输入地址
                    Request request = new Request
                            .Builder()
                            .url("http://www.yyjsx.xyz/api/message/listByCategory?messageCategory=WECHAT")
                           // .url("http://10.0.2.2/get_data.json")
                            .build();
                    // 发送请求，相当于在浏览器按回车
                    Response response = client.newCall(request).execute();
                    //更新listview

                    String responseDate = response.body().string();
                    //Log.d(TAG, body);
                    parseJson(responseDate);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void parseJson(String jsonStr){
        try {
            JSONObject jobj = new JSONObject(jsonStr);
            String dataStr = jobj.getString("data");

            Gson gson = new Gson();
            //更新(listview)UI.不能在子线程更新UI，只能在主线程更新UI
            final List<Message>messageList = gson.fromJson(dataStr, new TypeToken<List<Message>>(){}.getType());
            // 更新UI
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    MessageAdapter fa = new MessageAdapter(FirstActivity.this, R.layout.activity_message_item, messageList);
                    ListView lv = (ListView) findViewById(R.id.lv);
                    lv.setAdapter(fa);
                }
            });
           /* MessageAdapter adapter=new MessageAdapter(FirstActivity.this,R.layout.activity_message_item,messageList);
            ListView lv=(ListView)findViewById(R.id.lv);
            lv.setAdapter((ListAdapter) adapter);*/
            for (Message m : messageList){
                Log.d(TAG, m.getTitle());
                Log.d(TAG,m.getSummary());
               /* Log.d(TAG,"id is"+ m.getId());
                Log.d(TAG,"preview is"+ m.getTitle());
                Log.d(TAG,"title is"+ m.getId());
                Log.d(TAG,"summary is"+ m.getSummary());
                Log.d(TAG,"greatedate is"+ m.getCreateDate());*/
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

